/*
 * Created by JFormDesigner on Sun Oct 19 17:39:10 CST 2025
 */

package com.geek.javase.ch10.jformdesigntest.event;

import java.awt.*;
import java.awt.event.*;
import java.awt.event.KeyEvent;
import javax.swing.*;

/**
 * @author 27636
 */
public class EventCalculate extends JFrame {
    public static void main(String[] args) {
        new EventCalculate().setVisible(true);
    }

    public EventCalculate() {
        initComponents();
    }
    //求和方法
    public int sum(int i,int j){
        return i+j;
    }
    //鼠标点击事件
    private void button1ActionPerformed(ActionEvent e) {
        // TODO add your code here
        int i = Integer.parseInt(num1.getText());
        int j = Integer.parseInt(num2.getText());
        result.setText(sum(i,j)+"");
    }
    //键盘按下事件
    private void button1KeyPressed(KeyEvent e) {
        // TODO add your code here
        int i = Integer.parseInt(num1.getText());
        int j = Integer.parseInt(num2.getText());
        result.setText(sum(i,j)+"");
    }


    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        num1 = new JTextField();
        label = new JLabel();
        num2 = new JTextField();
        result = new JTextField();
        button1 = new JButton();

        //======== this ========
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setTitle("\u7b80\u6613\u8ba1\u7b97\u5668");
        Container contentPane = getContentPane();
        contentPane.setLayout(null);
        contentPane.add(num1);
        num1.setBounds(35, 60, 50, 30);

        //---- label ----
        label.setText("+");
        label.setHorizontalAlignment(SwingConstants.CENTER);
        contentPane.add(label);
        label.setBounds(100, 60, 20, 20);
        contentPane.add(num2);
        num2.setBounds(140, 60, 50, 30);
        contentPane.add(result);
        result.setBounds(280, 60, 50, 30);

        //---- button1 ----
        button1.setText("=");
        button1.addActionListener(e -> button1ActionPerformed(e));
        button1.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                button1KeyPressed(e);
            }
        });
        contentPane.add(button1);
        button1.setBounds(220, 60, 50, 30);

        contentPane.setPreferredSize(new Dimension(400, 300));
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JTextField num1;
    private JLabel label;
    private JTextField num2;
    private JTextField result;
    private JButton button1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
